package com.yksj.healthtalk.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.dictionary.DictionarySecendActivity;
import com.yksj.healthtalk.ui.doctor.DoctorSearchResultActivity;
import com.yksj.healthtalk.ui.friend.FriendSearchUI;
import com.yksj.healthtalk.ui.interestwall.InterestMainActivity;
import com.yksj.healthtalk.ui.salon.SalonMainUIActivity;
import com.yksj.healthtalk.ui.server.ServerCenterMainUI;
import com.yksj.healthtalk.ui.server.ServerShopSeachActivity;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;

public class MainSeachAllUI extends BaseFragmentActivity implements OnClickListener, OnItemClickListener {

	private ListView mListView;
	private myAdapter adapter;
	private EditText mTextContent;
	int type;//类型 1,全部搜索  2,服务中心0  3,保健
	private Button mSeach;//搜索按钮
	final DisplayMetrics metrics = new DisplayMetrics();

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.main_seach_all_layout);
		initTitle();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		mTextContent = (EditText) findViewById(R.id.text_content);
		mListView = (ListView) findViewById(R.id.listview);
		adapter = new myAdapter();
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);
		SystemUtils.hideSoftBord(getApplicationContext(), mTextContent);
		this.type=getIntent().getIntExtra("type",1);
		if(type==1){
			findViewById(R.id.title).setVisibility(View.GONE);
			findViewById(R.id.seach_layout).setVisibility(View.VISIBLE);
			mSeach = (Button) findViewById(R.id.seach);
			mSeach.setOnClickListener(this);
			mTextContent.addTextChangedListener(new TextWatcher() {
				
				@Override
				public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
					if(arg0.length()>0){
						seachAll(mTextContent.getText().toString());
					}else{
//						mSeach.setVisibility(View.GONE);
					}
				}
				
				@Override
				public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
						int arg3) {
				}
				
				@Override
				public void afterTextChanged(Editable arg0) {
				}
			});
			mTextContent.setText(getIntent().getStringExtra("content"));
			seachAll(getIntent().getStringExtra("content"));
		}else{
			findViewById(R.id.title).setVisibility(View.VISIBLE);
			findViewById(R.id.seach_layout).setVisibility(View.GONE);
			titleLeftBtn.setOnClickListener(this);
			seachAll("");
		}
//		findViewById(R.id.text_clean).setOnClickListener(this);
	}
	
	
	
	
	private void seachAll(String value){
		//OPTION=ALL&KEYWORDS=高
		RequestParams params=new RequestParams(); 
		switch (type) {
		case 1:
			params.put("OPTION", "ALL");
			params.put("KEYWORDS", mTextContent.getText().toString());
			break;
		case 2://服务中心
			params.put("OPTION", "YIYUAN");
			titleTextV.setText("医院机构");
			break;
		case 3://保健
			params.put("OPTION", "BAOJIAN");
			titleTextV.setText("保健服务");
			break;
		case 4:
			params.put("OPTION", "BAOJIAN");
			params.put("KEYWORDS", value);
			titleTextV.setText("保健服务");
			SystemUtils.hideSoftBord(getApplicationContext(), mTextContent);
			break;
		}
		HttpRestClient.doHttpHomePageQueryServlet(params,new JsonsfHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);
				if(response.containsKey("error_message")){
					ToastUtil.showShort(response.getString("error_message"));
				}else{
					JSONArray jsonArray = response.getJSONArray("RESULT");
					adapter.onBountData(type, jsonArray);
				}
			}
		});
	}
	
	
	
	private class myAdapter extends BaseAdapter{
		final JSONArray jsonArray;
		LayoutInflater inflater;
		private int type;
		
		public myAdapter() {
			super();
			this.inflater = LayoutInflater.from(MainSeachAllUI.this);
			jsonArray=new JSONArray();
		}

		@Override
		public int getCount() {
			return jsonArray.size();
		}
		
		public void onBountData(int type,JSONArray jsonArray){
			this.type=type;
			this.jsonArray.clear();
			this.jsonArray.addAll(jsonArray);
			notifyDataSetChanged();
		}
		
		@Override
		public JSONObject getItem(int arg0) {
			return (JSONObject) jsonArray.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ViewHolder holder;
			
			if(arg1==null){
				holder=new ViewHolder();
				arg1=onCreateView(type,holder);
				arg1.setTag(holder);
			}
			doBoundData(type,(ViewHolder)arg1.getTag(),getItem(arg0));
			
			return arg1;
		}
		
		public void doBoundData(int type, ViewHolder holder,JSONObject jsonObject) {
			switch (type) {
			case 1:
				holder.icon.setVisibility(View.VISIBLE);
				holder.name.setText(jsonObject.getString("NAME"));
				holder.number.setText(jsonObject.getString("COUNT"));
				String path=jsonObject.getString("ICON");
				if(path.contains("zhuanjia")){
					holder.icon.setBackgroundResource(R.drawable.seach_zhuanjia);
				}else if(path.contains("yaopin")){
					holder.icon.setBackgroundResource(R.drawable.seach_yaopin);
				}else if(path.contains("shalong")){
					holder.icon.setBackgroundResource(R.drawable.seach_salon);
				}else if(path.contains("bingyou")){
					holder.icon.setBackgroundResource(R.drawable.seach_bingyou);
				}else if(path.contains("fabu")){
					holder.icon.setBackgroundResource(R.drawable.seach_fabu);
				}else if(path.contains("zhengzhuang")){
					holder.icon.setBackgroundResource(R.drawable.seach_zhengzhuang);
				}else if(path.contains("jibing")){
					holder.icon.setBackgroundResource(R.drawable.seach_jibing);
				}else if(path.contains("baojian")){
					holder.icon.setBackgroundResource(R.drawable.seach_baojian);
				}else if(path.contains("yiyuan")){
					holder.icon.setBackgroundResource(R.drawable.seach_yiyuan);
				}else{
					holder.icon.setVisibility(View.GONE);
				}
				break;
			case 2:
				holder.icon.setVisibility(View.INVISIBLE);//MERCHANT_ID
				holder.name.setText(jsonObject.getString("MERCHANT_NAME"));
				holder.number.setText("");
				break;
			case 3:
			case 4:
				holder.icon.setVisibility(View.INVISIBLE);
				holder.name.setText(jsonObject.getString("MERCHANT_NAME"));
				holder.number.setText("");
				break;
			}
			
		} 
		
		
		public  class ViewHolder{
			public ImageView icon;
			public TextView name;
			public TextView number;
		}
		
		private View onCreateView(int type,ViewHolder holder){
			View view = null;
			switch(type){
			case 1://
			case 2://
			case 3://
				view = inflater.inflate(R.layout.main_seach_all_item,null);
				holder.icon=(ImageView) view.findViewById(R.id.icon_view);
				holder.name=(TextView) view.findViewById(R.id.name_type);
				holder.number=(TextView) view.findViewById(R.id.number);
				break;
			}
			return view;
		}
		
	}



	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.seach:
			String content=mTextContent.getText().toString();
			if(!TextUtils.isEmpty(content)){
				seachAll(content);
			}else{
				ToastUtil.showShort("请输入搜索内容");
			}
			break;
		case R.id.title_back:
			onBackPressed();
			break;
//		case R.id.text_clean:
//			mTextContent.setText("");
//			break;
		}
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch (type) {
		case 1:
			JSONObject object = adapter.getItem(arg2);
			String name=object.getString("NAME");
			if(name.contains("药品")){
				Intent intent=new Intent(this,ServerShopSeachActivity.class);
				intent.putExtra("name", mTextContent.getText().toString());
				startActivity(intent);
			}else if(name.contains("医生")){//专家医生
				Intent intent=new Intent(this,DoctorSearchResultActivity.class);
				intent.putExtra("result", mTextContent.getText().toString());
				startActivity(intent);
			}else if(name.contains("话题")){//话题讨论
				Intent intent=new Intent(this,SalonMainUIActivity.class);
				intent.putExtra("groupName", mTextContent.getText().toString());
				startActivity(intent);
			}else if(name.contains("病友")){//病友交流
				Intent intent=new Intent(this,FriendSearchUI.class);
				intent.putExtra("duoMeiNum", mTextContent.getText().toString());
				startActivity(intent);
			}else if(name.contains("发布")){//个人发布
				Intent intent=new Intent(this,InterestMainActivity.class);
				intent.putExtra("NAME", mTextContent.getText().toString());
				startActivity(intent);
				
			}else if(name.contains("疾病")){//疾病
				Intent intent = new Intent(this,DictionarySecendActivity.class);
				intent.putExtra("title", "");
				intent.putExtra("keyname",mTextContent.getText().toString());
				intent.putExtra("parame", "-100");
				startActivity(intent);
				
			}else if(name.contains("保健")){//保健服务

				Intent intent =new Intent(this,ServerCenterListActivity.class);
				intent.putExtra("type", 4);
				intent.putExtra("value", mTextContent.getText().toString());
				startActivity(intent);
				
			}else if(name.contains("症状")){
				Intent intent = new Intent(this,DictionarySecendActivity.class);
				intent.putExtra("title", "");
				intent.putExtra("keyname",mTextContent.getText().toString());
				intent.putExtra("parame", "-101");
				startActivity(intent);
			}else if(name.contains("医院")){
				Intent intent =new Intent(this,ServerCenterListActivity.class);
				intent.putExtra("type", 2);
				intent.putExtra("value", mTextContent.getText().toString());
				startActivity(intent);
			}else{
				
			}
			break;
		case 2:
		case 3:
		case 4:
			final String MERCHANT_ID=adapter.getItem(arg2).getString("MERCHANT_ID");
			HttpRestClient.doHttpServerCneterBg(String.valueOf(metrics.widthPixels), String.valueOf(metrics.heightPixels), SmartFoxClient.getLoginUserId(),MERCHANT_ID,
					new JsonsfHttpResponseHandler(this){
				@Override
				public void onSuccess(int statusCode,
						JSONObject response) {
					super.onSuccess(statusCode, response);
					String str = response.getString("VALID_FLAG");
					if ("2".equals(str)) {// 不可以进入
						SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),
								"即将开通,敬请期待!");
					} else {
						Intent intent=new Intent(MainSeachAllUI.this,ServerCenterMainUI.class);
						Bundle bundle=new Bundle();
						bundle.putString("content", response.toString());
						bundle.putString("MERCHANT_ID",MERCHANT_ID);
						intent.putExtras(bundle);
						startActivity(intent);
					}
				}
			});
			break;
		}
		
	}
}
